import { http } from "@/utils/http";

export class K8sWorkload {
  apiV1Url = (cluster_name, type) => {
    return `/api/v1/proxy/${cluster_name}/k8s/api/v1/${type}`;
  };
  apiV1UrlWithNsUrl = (cluster_name, type, namespaces) => {
    return `/api/v1/proxy/${cluster_name}/k8s/api/v1/namespaces/${namespaces}/${type}`;
  };
  appsV1Url = (cluster_name, type) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/apps/v1/${type}`;
  };
  appsV1UrlWithNsUrl = (cluster_name, type, namespaces) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/apps/v1/namespaces/${namespaces}/${type}`;
  };
  batchV1beta1Url = (cluster_name, type) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/batch/v1beta1/${type}`;
  };
  batchV1beta1WithNsUrl = (cluster_name, type, namespaces) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/batch/v1beta1/namespaces/${namespaces}/${type}`;
  };
  batchV1Url = (cluster_name, type) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/batch/v1/${type}`;
  };
  batchV1WithNsUrl = (cluster_name, type, namespaces) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/batch/v1/namespaces/${namespaces}/${type}`;
  };

  listWorkLoads(cluster_name, type, search, keywords, pageNum, pageSize) {
    let url;
    let params = {};
    switch (type) {
      case "deployments":
      case "statefulsets":
      case "daemonsets":
        url = this.appsV1Url(cluster_name, type);
        break;
      case "pods":
        url = this.apiV1Url(cluster_name, type);
        break;
      case "cronjobs":
        url = this.batchV1beta1Url(cluster_name, type);
        break;
      case "jobs":
        url = this.batchV1Url(cluster_name, type);
        break;
    }
    if (search) {
      params["search"] = true;
      if (keywords) {
        params["keywords"] = keywords;
      }
      if (pageNum && pageSize) {
        params["pageNum"] = pageNum;
        params["pageSize"] = pageSize;
      }
    }
    return http.get(url, params);
  }

  getWorkLoadByName(cluster_name, type, namespace, name) {
    switch (type) {
      case "deployments":
      case "statefulsets":
      case "daemonsets":
        return http.get(
          `${this.appsV1UrlWithNsUrl(cluster_name, type, namespace)}/${name}`
        );
      case "cronjobs":
        return http.get(
          `${this.batchV1beta1WithNsUrl(cluster_name, type, namespace)}/${name}`
        );
      case "pods":
        return http.get(
          `${this.apiV1UrlWithNsUrl(cluster_name, type, namespace)}/${name}`
        );
      case "jobs":
        return http.get(
          `${this.batchV1WithNsUrl(cluster_name, type, namespace)}/${name}`
        );
    }
  }

  deleteWorkLoad(cluster_name, type, namespace, name) {
    switch (type) {
      case "deployments":
      case "statefulsets":
      case "daemonsets":
        return http.delete(
          `${this.appsV1UrlWithNsUrl(cluster_name, type, namespace)}/${name}`
        );
      case "cronjobs":
        return http.delete(
          `${this.batchV1beta1WithNsUrl(cluster_name, type, namespace)}/${name}`
        );
      case "pods":
        return http.delete(
          `${this.apiV1UrlWithNsUrl(cluster_name, type, namespace)}/${name}`
        );
      case "jobs":
        return http.delete(
          `${this.batchV1WithNsUrl(cluster_name, type, namespace)}/${name}`
        );
    }
  }

  createWorkLoad(cluster_name, type, namespace, data) {
    switch (type) {
      case "deployments":
      case "statefulsets":
      case "daemonsets":
        return http.post(
          `${this.appsV1UrlWithNsUrl(cluster_name, type, namespace)}`,
          data
        );
      case "cronjobs":
        return http.post(
          `${this.batchV1beta1WithNsUrl(cluster_name, type, namespace)}`,
          data
        );
      case "pods":
      case "services":
        return http.post(
          `${this.apiV1UrlWithNsUrl(cluster_name, type, namespace)}`,
          data
        );
      case "jobs":
        return http.post(
          `${this.batchV1WithNsUrl(cluster_name, type, namespace)}`,
          data
        );
    }
  }

  updateWorkLoad(cluster_name, type, namespace, name, data) {
    switch (type) {
      case "deployments":
      case "statefulsets":
      case "daemonsets":
        return http.put(
          `${this.appsV1UrlWithNsUrl(cluster_name, type, namespace)}/${name}`,
          data
        );
      case "cronjobs":
        return http.put(
          `${this.batchV1beta1WithNsUrl(cluster_name, type, namespace)}/${name}`,
          data
        );
      case "pods":
        return http.put(
          `${this.apiV1UrlWithNsUrl(cluster_name, type, namespace)}/${name}`
        );
      case "jobs":
        return http.put(
          `${this.batchV1WithNsUrl(cluster_name, type, namespace)}/${name}`,
          data
        );
    }
  }
}
